// pages/add_address/add_address.js 
var app= getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        inputName:'',
        inputPhone:'',
        region:'',
        textArea:'',
        isDefault:false,
    },
    //选择默认
    chooseDefault:function(){
        var that=this;
        if(that.data.isDefault==false){
            that.setData({
                isDefault:true, 
            })
        }else{
            that.setData({
                isDefault:false,
            })
        }
    },
    bindRegionChange: function (e) {
        console.log('picker发送选择改变，携带值为', e.detail.value)
        this.setData({
          region: e.detail.value[0] + '-' + e.detail.value[1] + '-' +e.detail.value[2]
        })
    },
    getInputName:function(e){
        this.setData({
            inputName:e.detail.value,
        })
    },
    getInputPhone: function (e) {
        this.setData({
            inputPhone: e.detail.value,
        })
    },
    getTextArea: function (e) {
        this.setData({
            textArea: e.detail.value,
        })
    },
    //保存
    sure:function(){
        var that=this;
        var reg1 = new RegExp(/^[\u4E00-\u9FA5]{2,4}$/);
        var reg2 = new RegExp(/^1(3|4|5|7|8)\d{9}$/);
        if(!that.data.inputName){
            wx.showToast({
                title: '请输入姓名',
                icon:'none',
                duration:1500,
            })
        } else if (!reg1.exec(that.data.inputName)){
            wx.showToast({
                title: "姓名格式错误",
                icon: 'none',
                duration: 1500,
            })
        }else if(!that.data.inputPhone){
            wx.showToast({
                title: '请输入手机号',
                icon: 'none',
                duration: 1500,
            })
        }else if(!reg2.exec(that.data.inputPhone)) {
            wx.showToast({
                title: "手机号格式错误",
                icon: 'none',
                duration: 1500,
            })
        }else if (!that.data.region) {
            wx.showToast({
                title: '请选择所在区域',
                icon: 'none',
                duration: 1500,
            })
        }else if(!that.data.textArea) {
            wx.showToast({
                title: '请输入详细地址',
                icon: 'none',
                duration: 1500,
            })
        }else{
          wx.request({
            url: app.siteInfo.shoppath + 'addAddress',
            method: "post",
            data: {
              uid: app.siteInfo.uid,
              name: that.data.inputName,
              phone: that.data.inputPhone,
              location: that.data.region,
              address: that.data.textArea,
              isdefault: that.data.isDefault
            },
            success: function (res) {
              if(res.data.code == 200){
                wx.showToast({
                  title: '保存成功',
                  icon: 'success',
                  duration: 1500,
                })
                setTimeout(function(){
                  wx.navigateBack({
                      delta:1,
                  })
                },2000);
                
              }else{
                wx.showToast({
                  title: '保存失败',
                  duration: 1500,
                })
              }
            }
          })
        }
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

    },
    // 获取formid
    getFormId: function (e) {
      var formid = e.detail.formId;
      console.log(formid);
      wx.request({
        url: app.siteInfo.siteroot + 'saveformid',
        method: "post",
        data: {
          uid: app.siteInfo.uid,
          formid: formid
        },
        success: function (res) {

        }
      })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
  onShareAppMessage: function () {
    wx.request({
      url: app.siteInfo.siteroot + 'relay',
      method: "post",
      data: {
        uid: app.siteInfo.uid,
        pid: app.siteInfo.pid,
        replaysource: "index"
      },
      success: function (res) { }
    })
    return {
      title: app.siteInfo.sharetitle,
      path: "/pages/index/index?share_card_id=" + app.siteInfo.pid + "&shareid=" + app.siteInfo.uid,
      success: function (a) {
        wx.request({
          url: app.siteInfo.siteroot + 'relay',
          method: "post",
          data: {
            uid: app.siteInfo.uid,
            pid: app.siteInfo.pid
          },
          success: function (res) { }
        })
      },
      fail: function (a) { }
    };
  }
})